package com.quanfaqiang.demo1.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanfaqiang.demo1.anno.AddLog;
import com.quanfaqiang.demo1.anno.DeleteLog;
import com.quanfaqiang.demo1.anno.UpdateLog;
import com.quanfaqiang.demo1.pojo.Result;
import com.quanfaqiang.demo1.pojo.User;
import com.quanfaqiang.demo1.service.UserService;
import com.quanfaqiang.demo1.utils.JwtUtil;
import com.quanfaqiang.demo1.utils.Md5Util;
import com.quanfaqiang.demo1.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@CrossOrigin()
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate stringRedisTemplate;



    @PostMapping("/login")
    public Result login(String username, String password,String captcha, HttpServletRequest request) {
        String sessionCaptcha = (String) request.getSession().getAttribute("captcha");
        if (sessionCaptcha == null || !sessionCaptcha.equalsIgnoreCase(captcha)) {
            return Result.error("验证码错误");
        }
        System.out.println(username + ":" + password);
        User loginuser = userService.findByUserName(username);
        if (loginuser == null) {
            return Result.error("用户名不存在");
        }
        if (Md5Util.getMD5String(password).equals(loginuser.getPassword())) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("id", loginuser.getId());
            stringObjectHashMap.put("username", loginuser.getUsername());
            String token = JwtUtil.genToken(stringObjectHashMap);
            //把token存储到redisz中
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set("token",token);
            return Result.success(token);
        }

        return Result.error("密码错误");
    }
//    public ResponseEntity<?> login(@RequestParam String username, @RequestParam String password, @RequestParam String captcha, HttpServletRequest request) {
//        String sessionCaptcha = (String) request.getSession().getAttribute("captcha");
//        if (sessionCaptcha == null || !sessionCaptcha.equalsIgnoreCase(captcha)) {
//            return ResponseEntity.badRequest().body("验证码错误");
//        }
//        // 进行用户名和密码验证逻辑
//        return ResponseEntity.ok().body("登录成功");
//    }

    @AddLog
    @PostMapping("/register")
    public Result register(String username, String password) {
        User u = userService.findByUserName(username);
        System.out.println(u);
        if (u == null) {
            userService.register(username, password);
            return Result.success("注册成功");
        } else {
            return Result.error("用户名已被占用");
        }
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        System.out.println(map);
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);

    }

    @DeleteLog
    @PostMapping("/logoff")
    public Result logoff(String username, String password) {
        boolean logoff = userService.logoff(username, password);
        System.out.println(logoff);
        if (logoff) {
            System.out.println("注销成功");
        }else {
            System.out.println("用户名或密码不正确");
        }
        return Result.success(logoff);
    }


    @GetMapping("/SelectAll")
    public Result SelectAll(int currentPage, int pageSize) {
        Page<User> user = userService.SelectAll(currentPage, pageSize);
        System.out.println(user);
        return Result.success(user);
    }

    @DeleteLog
    @DeleteMapping("/deleteId/{id}")
    public Result<String> DeleteId(@PathVariable Integer id) {
        userService.DeleteId(id);
        return Result.success("删除成功");
    }

    @DeleteLog
    @DeleteMapping("/deleteMore/{ids}")
    public Result deleteIds(@PathVariable List<Integer> ids) {
        System.out.println(ids+"sssssssssssssssssss");
        userService.deleteIds(ids);
        return Result.success("商品批量删除成功");
    }


    @GetMapping("/selectId")
    public Result SelectId(@RequestParam Integer id) {
        User user = userService.SelectId(id);
        System.out.println(id);
        System.out.println(user);
        return Result.success(user);
    }

    @GetMapping("/selectName")
    public Result selectName(String username) {
        User loginuser = userService.findByUserName(username);
        System.out.println(loginuser);
        return Result.success(loginuser);
    }

    @AddLog
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        System.out.println(user);
        userService.Add(user);
        return Result.success("添加成功");
    }

    @GetMapping("/selectPassword")
    public Result selectpassword(String password, Integer id) {
        System.out.println(password+":"+id);
        Boolean flag = userService.selectPassword(password, id);
        System.out.println(flag);
        if (flag == true) {
            System.out.println("校验成功");
        }else {
            System.out.println("校验失败");
        }
        return  Result.success(flag);
    }

    @UpdateLog
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        System.out.println(user);
        userService.update(user);
        return Result.success("修改成功");
    }

    @UpdateLog
    @PatchMapping("/updatePwd")
    public Result updatePwd(String password, Integer id) {
        System.out.println(password+":"+id);
        userService.updatePwd(password, id);
        return Result.success("密码修改成功");
    }

    @UpdateLog
    @PatchMapping("/updateUserAddress")
    public Result updateUserAddress(String UserAddress, int id) {
        userService.updateUserAddress(UserAddress, id);
        return Result.success(id + "修改地址成功");
    }



    @UpdateLog
    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam  String avatarUrl){
        System.out.println(avatarUrl);
        userService.updateAvatar(avatarUrl);
        return Result.success("更新用户头像完成");
    }

    @GetMapping("/serchUserName")
    public Result searchGoods(String userName,Integer currentPage, Integer pageSize){
        Page<User> userList=userService.searchUser(userName,currentPage,pageSize);
        return Result.success(userList);
    }

    @GetMapping("/serchUser")
    public Result serchUser(String username,String name ,String address,String phone,String email,Boolean isadmin,Boolean isonline,Integer currentPage, Integer pageSize){
        System.out.println(username+"dddddddddddddddddddd"+":"+isonline);
        Page<User> userList=userService.serchUser(username,address,name,phone,email,isadmin,isonline,currentPage,pageSize);
        return Result.success(userList);
    }

    @GetMapping("/selectUsername")
    public Result selectUsername(String username){
        System.out.println(username);
        Boolean flag=userService.selectUsername(username);
        System.out.println(flag);
        return Result.success(flag);
    }

    @GetMapping("/selectUsername1")
    public Result selectUsername1(String username) {
        System.out.println(username);
        User user = userService.selectUsername1(username);
        return Result.success(user);
    }

}
